package com.jl.jlapp.mvp.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;

import com.jl.jlapp.R;
import com.jl.jlapp.eneity.AuthResult;
import com.jl.jlapp.eneity.AuthTask;
import com.jl.jlapp.eneity.LoginModel;
import com.jl.jlapp.eneity.MessageCodeModel;
import com.jl.jlapp.eneity.PayResult;
import com.jl.jlapp.nets.Api;
import com.jl.jlapp.nets.CommonObserver;
import com.jl.jlapp.nets.Net;
import com.jl.jlapp.popu.ConnectServerPopu;
import com.jl.jlapp.utils.ToastUtils;
import com.jl.jlapp.utils.Tools;
import com.tencent.connect.UserInfo;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.SendAuth.Req;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import java.util.Map;

import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity {
    private static final int SDK_AUTH_FLAG = 2;
    private static final int SDK_PAY_FLAG = 1;
    public static Tencent mTencent;
    IUiListener IListener = new BaseUiListener();
    private IWXAPI api;
    String beforMobile = "";
    ConnectServerPopu clearHistorySearhPopu;
    public OnClickListener confirmClick = new OnClickListener() {
        public void onClick(View paramAnonymousView) {
            LoginActivity.this.clearHistorySearhPopu.dismiss();
            Intent intent = new Intent("android.intent.action.DIAL", Uri.parse("tel:4006865856"));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            LoginActivity.this.startActivity(intent);
        }
    };
    @BindView(R.id.connect_server)
    TextView connect_server;
    long failureTime = -1L;
    @BindView(R.id.get_code)
    TextView get_code;
    String inputCode = "";
    @BindView(R.id.login)
    TextView login;
    @SuppressLint({"HandlerLeak"})
    private Handler mHandler = new Handler() {
        public void handleMessage(Message paramAnonymousMessage) {
            switch (paramAnonymousMessage.what) {

                case 1:
                    PayResult payResult = new PayResult((Map) paramAnonymousMessage.obj);
                    if (TextUtils.equals(payResult.getResult(), "9000")) {
                        Toast.makeText(LoginActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Toast.makeText(LoginActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
                default:
                    AuthResult authResult = new AuthResult((Map) paramAnonymousMessage.obj, true);
                    if ((TextUtils.equals(authResult.getResultStatus(), "9000")) && (TextUtils.equals(authResult.getResultCode(), "200"))) {
                        LoginActivity.this.login(authResult.getAlipayOpenId(), 3);
                        Toast.makeText(LoginActivity.this, "授权成功\n" + String.format("authCode:%s", new Object[]{authResult.getAuthCode()}), Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Toast.makeText(LoginActivity.this, "授权失败" + String.format("authCode:%s", new Object[]{authResult.getAuthCode()}), Toast.LENGTH_SHORT).show();

                    break;


            }
        }
    };
    String messageCode = "";
    String mobile = "";
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.phone_number)
    EditText phone_number;
    @BindView(R.id.QQ)
    LinearLayout qq;
    @BindView(R.id.tips)
    TextView tipsView;
    @BindView(R.id.VX)
    LinearLayout vx;
    @BindView(R.id.ZFB)
    LinearLayout zfb;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

    private void init() {
        handleTips();
        this.phone_number.setInputType(3);
        this.password.setInputType(2);
        this.get_code.setOnClickListener(new OnClickListener() {
            public void onClick(View paramAnonymousView) {
                LoginActivity.this.mobile = LoginActivity.this.phone_number.getText().toString().trim();
                LoginActivity.this.beforMobile = LoginActivity.this.mobile;
                if ("".equals(LoginActivity.this.mobile)) {
                    Toast.makeText(LoginActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!LoginActivity.this.isMobile(LoginActivity.this.mobile)) {
                    Toast.makeText(LoginActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                Net.get().getMessageCode(LoginActivity.this.mobile)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new CommonObserver<MessageCodeModel>() {
                            @Override
                            protected void onError(String errorMsg) {

                            }

                            @Override
                            protected void onSuccess(MessageCodeModel messageCodeModel) {
                                ToastUtils.show(messageCodeModel.getMsg());
                                if (messageCodeModel.getCode() == 200) {
                                    messageCode = messageCodeModel.getResultData().getMessageCode();
                                    failureTime = messageCodeModel.getResultData().getFailureTime();
                                }
                            }
                        });
            }
        });
        this.login.setOnClickListener(new OnClickListener() {
            public void onClick(View paramAnonymousView) {
                LoginActivity.this.inputCode = LoginActivity.this.password.getText().toString().trim();
                LoginActivity.this.mobile = LoginActivity.this.phone_number.getText().toString().trim();
                long l = System.currentTimeMillis();
                if ("".equals(LoginActivity.this.mobile)) {
                    Toast.makeText(LoginActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();

                }
                if (!LoginActivity.this.isMobile(LoginActivity.this.mobile)) {
                    Toast.makeText(LoginActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if ("".equals(LoginActivity.this.messageCode)) {
                    Toast.makeText(LoginActivity.this, "请先获取动态密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if ("".equals(LoginActivity.this.inputCode)) {
                    Toast.makeText(LoginActivity.this, "请输入动态密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (LoginActivity.this.failureTime == -1L) {
                    Toast.makeText(LoginActivity.this, "请先获取动态密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (l > LoginActivity.this.failureTime) {
                    Toast.makeText(LoginActivity.this, "动态密码已失效，请重新获取", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (LoginActivity.this.inputCode.equals(LoginActivity.this.messageCode)) {
                    if (!LoginActivity.this.beforMobile.equals(LoginActivity.this.mobile)) {
                        Toast.makeText(LoginActivity.this, "手机号与动态密码不匹配，请重试", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Net.get().login(LoginActivity.this.mobile, 0)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new CommonObserver<LoginModel>() {
                                @Override
                                protected void onError(String errorMsg) {

                                }

                                @Override
                                protected void onSuccess(LoginModel loginModel) {
                                    if (loginModel.getCode() == 200) {
                                        if (sharedPreferences == null) {
                                            sharedPreferences = getSharedPreferences("JLAppData", 0);
                                            editor = sharedPreferences.edit();
                                        }
                                        editor.putString("mobile", loginModel.getResultData().getUserPhone()).commit();
                                        editor.putString("userPicUrl", loginModel.getResultData().getUserPicUrl()).commit();
                                        editor.putString("userName", loginModel.getResultData().getUserName()).commit();
                                        editor.putInt("userId", loginModel.getResultData().getUserId()).commit();
                                        finish();
                                    }
                                }
                            });
                } else {
                    Toast.makeText(LoginActivity.this, "动态密码错误", Toast.LENGTH_SHORT).show();
                }

            }
        });
        this.connect_server.setOnClickListener(new OnClickListener() {
            public void onClick(View paramAnonymousView) {
                LoginActivity.this.clearHistorySearhPopu = new ConnectServerPopu(LoginActivity.this, LoginActivity.this.confirmClick);
                LoginActivity.this.clearHistorySearhPopu.showAtLocation(LoginActivity.this.findViewById(R.id.activity_login), 17, 0, 0);
            }
        });
        this.vx.setOnClickListener(new OnClickListener() {
            public void onClick(View paramAnonymousView) {
                Toast.makeText(LoginActivity.this, "微信...", Toast.LENGTH_SHORT).show();
                LoginActivity.this.doLogin(2);
            }
        });
        this.qq.setOnClickListener(new OnClickListener() {
            public void onClick(View paramAnonymousView) {
                LoginActivity.this.doLogin(1);
                Toast.makeText(LoginActivity.this, "QQ...", Toast.LENGTH_SHORT).show();
            }
        });
        this.zfb.setOnClickListener(new OnClickListener() {
            public void onClick(View paramAnonymousView) {
                Toast.makeText(LoginActivity.this, "支付宝...", Toast.LENGTH_SHORT).show();
                LoginActivity.this.doLogin(3);
            }
        });
    }

    private void login(String paramString, int paramInt) {
        Log.d("loginActivity", "login:account: " + paramString);
        Net.get().login(paramString, paramInt).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<LoginModel>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(LoginModel loginModel) {

                    }
                });
    }

    public void doLogin(int paramInt) {
        switch (paramInt) {

            case 1:
                mTencent = Tencent.createInstance("1106681667", getApplicationContext());
                mTencent.login(this, "all", this.IListener);
                return;
            case 2:
                Tools.addActivity(this);
                SendAuth.Req localReq = new SendAuth.Req();
                localReq.scope = "snsapi_userinfo";
                localReq.state = "wechat_sdk_微信登录";
                this.api.sendReq(localReq);
                return;

            default:
                Net.get().getAuthInfo().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new CommonObserver<AuthTask>() {
                            @Override
                            protected void onError(String errorMsg) {

                            }

                            @Override
                            protected void onSuccess(AuthTask authTask) {

                            }
                        });
                return;
        }

    }

    public void handleTips() {
        String str = this.tipsView.getText().toString().trim();
        SpannableStringBuilder localSpannableStringBuilder = new SpannableStringBuilder(str);
        localSpannableStringBuilder.setSpan(new ClickableSpan() {
            public void onClick(View paramAnonymousView) {
                Intent intent = new Intent(LoginActivity.this, RegistBookActivity.class);
                LoginActivity.this.startActivity(intent);
            }

            public void updateDrawState(TextPaint paramAnonymousTextPaint) {
                super.updateDrawState(paramAnonymousTextPaint);
                paramAnonymousTextPaint.setColor(LoginActivity.this.getResources().getColor(R.color.black));
                paramAnonymousTextPaint.setUnderlineText(false);
                paramAnonymousTextPaint.clearShadowLayer();
            }
        }, str.length() - 6, str.length(), 33);
        this.tipsView.setText(localSpannableStringBuilder);
        this.tipsView.setMovementMethod(LinkMovementMethod.getInstance());
    }

    public boolean isMobile(String paramString) {
        if (TextUtils.isEmpty(paramString)) {
            return false;
        }
        return paramString.matches("[1][34578]\\d{9}");
    }

    protected void onActivityResult(int paramInt1, int paramInt2, Intent paramIntent) {
        if ((paramInt1 == 11101) || (paramInt1 == 10102)) {
            Tencent.onActivityResultData(paramInt1, paramInt2, paramIntent, this.IListener);
        }
        if (paramInt2 == 0) {
            login(paramIntent.getStringExtra("Openid"), 1);
        }
        super.onActivityResult(paramInt1, paramInt2, paramIntent);
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_login);
        Tools.addActivity(this);
        ButterKnife.bind(this);
        this.api = WXAPIFactory.createWXAPI(this, "wx037454184aee70e9", true);
        this.api.registerApp("wx037454184aee70e9");
        init();
        Intent intent = getIntent();
        if (intent.getIntExtra("type", 0) == 1) {
            login(intent.getStringExtra("Openid"), 1);
        }
    }

    private class BaseUiListener
            implements IUiListener {
        private BaseUiListener() {
        }

        protected void doComplete(JSONObject paramJSONObject) {
            Log.d("qqqqqq", paramJSONObject.toString());
        }

        public void onCancel() {
            Toast.makeText(LoginActivity.this, "登录关闭", Toast.LENGTH_SHORT).show();
        }

        public void onComplete(Object paramObject) {
            if (paramObject == null) {
                Toast.makeText(LoginActivity.this, "返回为空，登录失败", Toast.LENGTH_SHORT).show();
            }
            Object localObject4 = (JSONObject) paramObject;
            if ((localObject4 != null) && (((JSONObject) localObject4).length() == 0)) {
                Toast.makeText(LoginActivity.this, "返回为空，登录失败", Toast.LENGTH_SHORT).show();
                return;
            }
            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
            paramObject = null;
            String str = null;
            Object localObject3 = null;
            Object localObject1 = str;
            try {
                String access_token = ((JSONObject) localObject4).getString("access_token");
                String expires_in = ((JSONObject) localObject4).getString("expires_in");
                String openid = ((JSONObject) localObject4).getString("openid");

                if (!TextUtils.isEmpty(access_token) && !TextUtils.isEmpty(openid)) {
                    LoginActivity.mTencent.setAccessToken(access_token, expires_in);
                    LoginActivity.mTencent.setOpenId(openid);
                    new UserInfo(LoginActivity.this, LoginActivity.mTencent.getQQToken()).getUserInfo(new IUiListener() {
                        public void onCancel() {
                        }

                        public void onComplete(Object paramAnonymousObject) {
                            try {
                                paramAnonymousObject = (JSONObject) paramAnonymousObject;
                                paramAnonymousObject = LoginActivity.mTencent.getOpenId();
                                Log.d("loginActivity", "onComplete:qqOpenId: " + (String) paramAnonymousObject);
                                LoginActivity.this.login((String) paramAnonymousObject, 2);
                                return;
                            } catch (com.alibaba.fastjson.JSONException e) {
                            }
                        }

                        public void onError(UiError paramAnonymousUiError) {
                        }
                    });
                }

            } catch (org.json.JSONException localJSONException) {
            }


        }

        public void onError(UiError paramUiError) {
            Toast.makeText(LoginActivity.this, "ERROR" + paramUiError.errorDetail, Toast.LENGTH_SHORT).show();
        }
    }
}


/* Location:              /Users/davidzhang/Documents/decompileApk/jlfood-dex2jar.jar!/com/jl/jlapp/mvp/activity/LoginActivity.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */